﻿using IpToBlack.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IpToBlack.PUB
{
    public class EventLogMonitor
    {
        private string logName;
        private string logSource = "MyAppSource";

        public EventLogMonitor(string log)
        {
            logName = log;
            if (!EventLog.SourceExists(logSource))
            {
                EventLog.CreateEventSource(logSource, log);
                Console.WriteLine("事件源创建成功。");
            }
            //EventLog.CreateEventSource(logSource, log); // 创建事件源，如果已存在则不影响
        }

        public void StartMonitoring()
        {
            EventLog myLog = new EventLog();
            myLog.Log = logName;

            // 注册EventRecorded事件
            myLog.EntryWritten += new EntryWrittenEventHandler(MyOnEntryWritten);

            // 开始监听
            myLog.EnableRaisingEvents = true;
        }

        private void MyOnEntryWritten(object source, EntryWrittenEventArgs e)
        {
            EventLogEntry entry = e.Entry;
            //Console.WriteLine("New log entry:");
            //Console.WriteLine("Entry Type: " + entry.EntryType);
            //Console.WriteLine("Message: " + entry.Message);
            //Console.WriteLine("Time Written: " + entry.TimeGenerated);
            // 处理新事件

            if (entry.Source.Equals("MSSQLSERVER") &&
                        (entry.EventID == 18456 || entry.EventID == 18457 || entry.EventID == 18458) )
            {
                //调试用
                //Console.WriteLine("Entry Type: " + entry.EntryType);
                //Console.WriteLine("Time Generated: " + entry.TimeGenerated);
                //Console.WriteLine("Message: " + entry.Message);

                PUB.gFunc.WriteLog($"Date: {entry.TimeGenerated}");
                PUB.gFunc.WriteLog($"User: {string.Join("-", entry.ReplacementStrings)}");

                PUB.gFunc.WriteLog($"Message: " + entry.Message);

                var ip = PUB.gFunc.GetTextToIP(entry.Message);
                PUB.gFunc.WriteLog($"IP: " + ip);

                if (!string.IsNullOrEmpty(ip))
                {
                    DB db = new DB();

                    var item99 = db.iSugar.Queryable<sys_t_ip>().Where(b => b.ip == ip).First();
                    if (item99 != null)
                    {
                        item99.qty = item99.qty + 1;
                        db.UpdateModel(item99);
                    }
                    else
                    {
                        var item1 = new sys_t_ip()
                        {
                            ip = ip,
                            qty = 1,
                            b_flag = "0",
                        };
                        db.InsertModel(item1);
                    }
                }

            }

        }

        ~EventLogMonitor()
        {
            EventLog.DeleteEventSource(logSource);
        }
    }
}
